<template>
	<view class="wallet-box">
		<view @click="goWallet">
			<image :src="imgUrl + '/static/images/walletone.png'" mode=""></image>
			<view class="wallet-btn">{{ userDetail.openHuipayAccount && userDetail.openHuipayAccount == 2 ? '钱包管理' : '开通钱包' }}</view>
		</view>
		<u-overlay :show="loading">
			<view class="warp">
				<u-loading-icon :show="true"></u-loading-icon>
			</view>
		</u-overlay>
	</view>
</template>

<script>
import { getInfo } from '@/api/login.js';
import config from '@/config.js';
import { getUserFree, payFree, accountOpen, walletUrl } from '@/api/account/wallet.js';
export default {
	data() {
		return {
			// 用户详情
			userDetail: {},
			//需要查询支付手续费，true是需要查询，false不需要查询，默认需要查询
			needSelectOpenFree: true,
			loading: false,
			imgUrl: config.imgUrl,
			baseUrl: 'https://gogoduck.cn/#/pages/wallet/wallet'
		};
	},
	created() {
		this.getUserInfo();
	},
	methods: {
		//获取用户信息
		async getUserInfo() {
			const res = await getInfo();
			this.userDetail = res.data;

			if (res.data.openHuipayAccount == 2) {
				this.needSelectOpenFree = false;
			}
		},
		//查询用户是否付开户手续费了
		async getFree() {
			const res = await getUserFree();
			this.loading = false;
			if (!res.data) {
				//需要支付手续费。
				this.payFree();
			} else {
				// 不需要支付开户手续费了，直接调用开户接口
				this.accountOpen();
			}
		},
		// 用户支付手续费
		async payFree() {
			if (process.env.UNI_PLATFORM === 'h5') {
				// 获取应用启动时的参数
				let requestUrl = this.baseUrl;
				// 测试
				const res = await payFree(requestUrl);
				window.location.href = res.data;
			}
			let requestUrl = ''; //默认为null
			const res = await payFree(requestUrl);
			if (res.code == 200) {
				uni.navigateTo({
					url: '/pages/webView/webView?toUrl=' + res.data + '&payFlag=3'
				});
			}
			this.loading = false;
		},
		//用户开户
		async accountOpen() {
			if (process.env.UNI_PLATFORM === 'h5') {
				let requestUrl = this.baseUrl;
				const res = await accountOpen(requestUrl);
				window.location.href = res.data.redirectUrl;
			}
			let requestUrl = ''; //默认为null
			const res = await accountOpen(requestUrl);
			uni.navigateTo({
				url: '/pages/webView/webView?toUrl=' + res.data.redirectUrl
			});
			this.loading = false;
		},

		goWallet() {
			this.loading = true;
			let info = this.userDetail;
			if (!info) {
				this.$modal.showToast('获取用户信息失败，请返回从新进入！');
			}
			if (info.isRealName != 1 && !info.idNumber) {
				this.$modal.showToast('请实名后再进行操作！');
				this.loading = false;
				return;
			}
			if (this.needSelectOpenFree) {
				//需要查询一下是否需要支付开户费用，如果需要直接跳转开户页面
				this.getFree();
			} else {
				// 进入钱包管理
				this.toWallet();
			}
		},
		async toWallet() {
			if (process.env.UNI_PLATFORM === 'h5') {
				// 获取应用启动时的参数
				let requestUrl = this.baseUrl; //默认为null
				// 测试
				const res = await walletUrl(requestUrl);
				console.log(res);
				window.location.href = res.data.redirectUrl;
			}

			let requestUrl = ''; //默认为null
			// 测试
			const res = await walletUrl(requestUrl);
			uni.navigateTo({
				url: '/pages/webView/webView?toUrl=' + res.data.redirectUrl
			});

			this.loading = false;
		}
	}
};
</script>

<style lang="scss" scoped>
.warp {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 100%;
}
.wallet-box {
	width: 680rpx;
	height: 300rpx;
	border-radius: 30rpx;
	margin-left: 34.88rpx;
	position: relative;
	image {
		width: 680rpx;
		height: 300rpx;
		border-radius: 30rpx;
	}
	.wallet-btn {
		width: 160rpx;
		font-weight: 500;
		background: linear-gradient(90deg, #3adfeb 0%, #4463ff 100%) !important;
		height: 60rpx;
		border-radius: 10rpx;
		color: #fff;
		text-align: center;
		line-height: 65rpx;
		position: absolute;
		bottom: 75rpx;
		right: 20rpx;
		font-family: 'font';
	}
}
</style>
